//
//  EditViewController.m
//  WeiMo
//
//  Created by weixian he on 12-9-11.
//  Copyright (c) 2012年 . All rights reserved.
//

#import "EditViewController2.h"
#import <QuartzCore/QuartzCore.h>
#import "StringInputTableViewCell.h"
#import "DateInputTableViewCell.h"
#import "IntegerInputTableViewCell.h"
#import "TextFieldTableViewCell.h"
#import "FMManager.h"
#import "InterfaceDefine.h"
#import "ThreeImageCell.h"
#import "ThreeImageCell.h"
#import "DLog.h"
#import "SDImageCache.h"
#import "KeyValueObject.h"
#import "TouchImageView.h"
#import "TextFieldTableViewCell.h"
#import "SVSegmentedControl.h"
#import "InterfaceDefine.h"
#import "JSONKit.h"
#import "FMManager.h"
#import "AGImagePickerController.h"
#import "AGIPCToolbarItem.h"


#import <AssetsLibrary/AssetsLibrary.h>
#import "DemoImageEditor.h"
#import "UIImageView+WebCache.h"

#define kImageIntNums 4
#define kImageViewTagBase 10
#define kEconomicHeaderImageTag 9

#define kMaxPhotoNumber 8

#define kTextFieldTagBase 1000

@interface EditViewController2 ()
{
    UITextField *mCurTextField;
    int mTextFieldTagIndex;
    int mSex; //性别  1 男  2女
    MBProgressHUD *mHUD;
    int mSelDeleteImageIndex;
}

@property(nonatomic,retain) UIView *section0footView;

@property(nonatomic,retain) NSMutableArray *section0list;//图片
@property(nonatomic,retain) NSMutableArray *section1List;//个人介绍
@property(nonatomic,retain) NSMutableArray *textFieldList1;//个人介绍

@property(nonatomic,copy) NSString *modelsStyle;//个人风格
@property(nonatomic,copy) NSString *modelsPersent;//个人/公司介绍
@property(nonatomic,copy) NSString *modelsExperience;//演出经历
@property(nonatomic,copy) NSString *modelsPhotostr; //图片id
@property(nonatomic,copy) NSString *economicAvatarstr; //头像

@property(nonatomic,retain) NSMutableArray *section4List;//工作联系方式
@property(nonatomic,retain) NSMutableArray *inputList;

@property(nonatomic,retain) UILabel *section2headerView;
@property(nonatomic,retain) UILabel *section3headerView;
@property(nonatomic,retain) UILabel *section4headerView;
@property(nonatomic,retain) UILabel *section5headerView;

@property(nonatomic,retain) NSMutableDictionary *editboxDic;
@property(nonatomic,retain) SVSegmentedControl *sVSegmentedControl;

@property(nonatomic,retain) NSMutableDictionary *saveDataDic;

@property(nonatomic, retain) NSMutableArray *selectedPhotos; //选择图片

@property(nonatomic,retain) NSMutableDictionary *curModelDic;  //经纪人下的模特dic

@property(nonatomic,retain) DemoImageEditor *imageEditor;
@property(nonatomic,retain) ALAssetsLibrary *library;
@property(nonatomic,retain) UIImage *saveImage;

@property (nonatomic,copy) NSString *edit2UserId;
@end


@implementation EditViewController2
@synthesize dataDic;
@synthesize delegate;

@synthesize section0footView;

@synthesize section0list,section1List,textFieldList1;

@synthesize modelsStyle,modelsPersent,modelsExperience,economicAvatarstr,modelsPhotostr;

@synthesize inputList,section4List;

@synthesize section2headerView,section3headerView,section4headerView,section5headerView;

@synthesize editboxDic,saveDataDic;
@synthesize sVSegmentedControl;

@synthesize selectedPhotos;
@synthesize library = _library;
@synthesize imageEditor = _imageEditor;
@synthesize saveImage = _saveImage;
@synthesize curModelDic;
@synthesize edit2UserId;

- (void)dealloc
{
    self.dataDic = nil;
    self.delegate = nil;
    
    self.section0footView = nil;
    
    self.section0list = nil;
    self.section1List = nil;
    self.textFieldList1 = nil;
    
    self.modelsPersent = nil;
    self.modelsStyle = nil;
    self.modelsExperience = nil;
    self.modelsPhotostr = nil;
    self.economicAvatarstr = nil;
    
    self.inputList = nil;
    self.section4List = nil;
    
    self.section2headerView = nil;
    self.section3headerView = nil;
    self.section4headerView = nil;
    self.section5headerView = nil;
    
    self.editboxDic = nil;
    self.saveDataDic = nil;
    
    self.sVSegmentedControl = nil;
    
    self.selectedPhotos = nil;
    self.library = nil;
    self.imageEditor = nil;
    self.saveImage = nil;
    
    self.edit2UserId = nil;
    [super dealloc];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[UIBarButtonItem appearance] setTintColor:[UIColor blackColor]];
    
    self.edit2UserId = @"";
    self.modelsPhotostr = @"";
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(editedAndToSaveNomodelDatas:)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release];
    
    self.tableView.backgroundColor = [UIColor underPageBackgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self initSectionHeaderView];
    [self initSections];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    FMManager *fm = [FMManager sharedInstance];
    if(fm.updateSecondCtrl)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    [self hideTabBar];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



#pragma mark - private

//隐藏TabBar
- (void)hideTabBar
{
    if (self.tabBarController.tabBar.hidden == YES)
    {
        return;
    }
    UIView *contentView;
    if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x,  contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
    self.tabBarController.tabBar.hidden = YES;
}

- (void)editedAndToSaveNomodelDatas:(id)sender
{
    NSMutableDictionary *dic = [[[NSMutableDictionary alloc] init] autorelease];
    
    if (self.economicAvatarstr)
    {
        [dic setValue:self.economicAvatarstr forKey:@"economicAvatarstr"];
    }
    
    NSString *economicId = [self.dataDic objectForKey:@"economicId"];
    [dic setValue:economicId forKey:@"economicId"];
    
    UITextField *textField;
    int index = 0;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsName = textField.text;
    if(!modelsName || [modelsName isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"个人/企业名称不能为空"];
        return;
    }
    [dic setObject:modelsName forKey:@"economicName"];
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsBloodtype = textField.text;
    if(!modelsBloodtype || [modelsBloodtype isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"职业/职位不能为空"];
        return;
    }
    [dic setObject:modelsBloodtype forKey:@"economicBloodtype"];
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsAddress = textField.text;
    if(!modelsAddress || [modelsAddress isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"所在城市不能为空"];
        return;
    }
    [dic setObject:modelsAddress forKey:@"economicAddress"];
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsSsgs = textField.text;
    /*
    if([modelsAddress isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"所属公司不能为空"];
        return;
    }
     */
    [dic setObject:modelsSsgs forKey:@"economicSsgs"];
    
    
    int inputTextIndex=0;
    UIPlaceHolderTextView *textView;
    TextFieldTableViewCell *cell = [self.inputList objectAtIndex:inputTextIndex];
    textView= cell.textField;
    NSString *modelsStyleStr = textView.text;
    if(!modelsStyleStr || [modelsStyleStr isEqualToString:@""])
    {
        [FMManager showAlertView:@"个人风格不能为空"];
        return;
    }
    else
    {
        if (modelsStyleStr.length > 500)
        {
            [FMManager showAlertView:@"个人风格不能超出500字符"];
            return;
        }
        else
        {
            [dic setObject:modelsStyleStr forKey:@"economicStyle"];
        }
    }
    
    
    inputTextIndex++;
    cell = [self.inputList objectAtIndex:inputTextIndex];
    textView= cell.textField;
    NSString *modelsPersentStr = textView.text;
    if(!modelsPersentStr || [modelsPersentStr isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"个人/企业介绍不能为空"];
        return;
    }
    else
    {
        if (modelsPersentStr.length > 500)
        {
            [FMManager showAlertView:@"个人风格不能超出500字符"];
            return;
        }
        else
        {
            [dic setObject:modelsPersentStr forKey:@"economicPersent"];
        }
    }
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsPhone = textField.text;
    if(!modelsPhone || [modelsPhone isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"工作电话不能为空"];
        return;
    }
    else
    {
        if (modelsPhone.length < 20)
        {
            [dic setObject:modelsPhone forKey:@"economicPhone"];
        }
        else
        {
            [FMManager showAlertView:@"工作电话为少于20位的数字"];
            return;
        }
    }
    
    index++;
    textField= [self.textFieldList1 objectAtIndex:index];
    NSString *modelsEmail = textField.text;
    if(!modelsEmail || [modelsEmail isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"工作邮箱不能为空"];
        return;
    }
    else
    {
        NSString *prefor = @"(^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$)";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",prefor];
        BOOL isMatch = [predicate evaluateWithObject:modelsEmail];
        if (isMatch)
        {
            [dic setObject:modelsEmail forKey:@"economicEmail"];
        }
        else
        {
            [FMManager showAlertView:@"邮箱格式不争取,请正确输入"];
            return;
        }
    }
    
//    index++;
//    textField= [self.textFieldList1 objectAtIndex:index];
//    NSString *modelsQq = textField.text;
//    if(!modelsQq || [modelsQq isEqualToString:@""])
//    {
//        [FMManager showAlertViewTitle:@"" message:@"工作QQ不能为空"];
//        return;
//    }
//    [dic setObject:modelsQq forKey:@"economicQq"];
//    
//    index++;
//    textField= [self.textFieldList1 objectAtIndex:index];
//    NSString *modelsVx = textField.text;
//    if(!modelsVx || [modelsVx isEqualToString:@""])
//    {
//        [FMManager showAlertViewTitle:@"" message:@"工作微信不能为空"];
//        return;
//    }
//    [dic setObject:modelsVx forKey:@"economicVx"];
    
    
    inputTextIndex++;

    cell = [self.inputList objectAtIndex:inputTextIndex];
    textView= cell.textField;
    NSString *modelsExperienceStr = textView.text;
    /*
    if([modelsExperience isEqualToString:@""])
    {
        [FMManager showAlertViewTitle:@"" message:@"演出经历不能为空"];
        return;
    }
     */
    
    NSString *modelIdentify = [[FMManager sharedInstance] modeIdentify];
    if ([modelIdentify isEqualToString:@"3"])
    {
        modelIdentify = @"161";
    }
    else if ([modelIdentify isEqualToString:@"4"])
    {
        modelIdentify = @"181";
    }
    [dic setObject:modelIdentify forKey:@"economicFirstType"];
    
    [dic setObject:modelsExperienceStr forKey:@"economicExperience"];

    NSString *userid = [[FMManager sharedInstance] userId]; //[self.dataDic objectForKey:@"userId"];
    [dic setObject:userid forKey:@"userId"];
    
    self.saveDataDic = dic;
    
    NSString *postStr = [dic JSONString];
    DLog(@"%@",postStr);
    
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",kHttpHost,kUpdateNomodelDatas];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
    request.delegate = self;
    [request startAsynchronous];
    
    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:mHUD];
    mHUD.delegate = self;
    [mHUD show:YES];
}

-(void)initSectionHeaderView
{
    self.section2headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
    self.section2headerView.text = @"  个人风格:";
    self.section2headerView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
    self.section2headerView.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
    
    self.section3headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
    self.section3headerView.text = @"  个人/企业介绍:";
    self.section3headerView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
    self.section3headerView.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
    
    
    self.section4headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
    self.section4headerView.text = @"  联系方式(发送资料显示):";
    self.section4headerView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
    self.section4headerView.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
    
    self.section5headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
    self.section5headerView.text = @"  演出经历/企业主营:";
    self.section5headerView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
    self.section5headerView.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
    
    /*
     self.headerView2 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
     self.headerView2.text = @"   风格特点";
     self.headerView2.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1];
     self.headerView2.textColor = [UIColor colorWithRed:65.0/255 green:65.0/255 blue:65.0/255 alpha:1];
     */
}

- (void)initSections
{
    self.inputList = [[[NSMutableArray alloc] init] autorelease];
    
    //section 0 图片
    //modelsPhotostr,economicPhotostr
    self.economicAvatarstr = [self.dataDic objectForKey:@"economicAvatarstr"];
    
    self.modelsPhotostr = [self.dataDic objectForKey:@"economicPhotostr"];
    
    DLog(@"modelsPhotostr= %@",self.modelsPhotostr);
    NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];
    if(![self.modelsPhotostr isEqualToString:@""])
    {
        NSRange range = [self.modelsPhotostr rangeOfString:@";"];
        if (range.location != NSNotFound)
        {
            NSArray *strPhotos = [self.modelsPhotostr componentsSeparatedByString:@";"];
            
            /*
             canClick:推荐
             isCheck:预约
             key:用户id
             keyDispName:小图url
             */
            for( int i=0; i<strPhotos.count; i++)
            {
                KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
                item.canClick = YES;
                item.isCheck = YES;
                
                item.keyDispName = [strPhotos objectAtIndex:i];
                //NSString *imageUrl = [NSString stringWithFormat:@"%@/%@/%@/%@",kHttpHost,@"photo",fm.userId,item.key];
                //DLog(@"imageUrl= %@",imageUrl);
                //item.keyDispName = imageUrl;
                [tempArray addObject:item];
            }
        }
        else
        {
            KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
            item.canClick = YES;
            item.isCheck = YES;
            
            item.keyDispName = self.modelsPhotostr;
            //NSString *imageUrl = [NSString stringWithFormat:@"%@/%@/%@/%@",kHttpHost,@"photo",fm.userId,item.key];
            //DLog(@"imageUrl= %@",imageUrl);
            //item.keyDispName = imageUrl;
            [tempArray addObject:item];
        }
    }
    
    self.section0list = tempArray;
    
    
    if(self.section0list.count < 8)
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"add";
        item.keyDispName = @"addphotos.png";
        [self.section0list addObject:item];
    }
    
//    NSArray *modelsList = [self.dataDic objectForKey:@"modelsList"];
//    
//    self.section0list = [[[NSMutableArray alloc] init] autorelease];
//    self.edit2UserId = [self.dataDic objectForKey:@"userId"];
//    //section 0 图片
//    if(modelsList && modelsList.count > 0)
//    {
//        for( int i=0; i<modelsList.count; i++)
//        {
//            KeyValueObject *item = [[KeyValueObject alloc] init];
//            item.canClick = YES;
//            item.isCheck = NO;
//            item.key = [NSString stringWithFormat:@"%d",i];
//            
//            NSDictionary *dicdata = [modelsList objectAtIndex:i];
//            NSString *imageUrl = [NSString stringWithFormat:@"%@/%@/%@/small_%@",kHttpHost,@"photo",self.edit2UserId,[dicdata objectForKey:@"modelsAvatarstr"]];
//            item.keyDispName = imageUrl;
//            [self.section0list addObject:item];
//            [item release];
//        }
//    }
//
//    if(self.section0list.count < kMaxPhotoNumber)
//    {
//        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
//        item.key = @"add";
//        item.keyDispName = @"addphotos2.png";
//        [self.section0list addObject:item];
//    }

    
    //section1 个人介绍
    self.section1List = [[[NSMutableArray alloc] init] autorelease];
    self.textFieldList1 = [[[NSMutableArray alloc] init] autorelease];
    
    //昵称
    [self.section1List addObject:@"名称:"];
    NSString *modelsName = [self.dataDic objectForKey:@"economicName"];
    mTextFieldTagIndex = kTextFieldTagBase;
    CGRect rect = CGRectMake(60, 4, 140, 28);
    {
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = modelsName;
        textField.delegate = self;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }
    
    //职业
    [self.section1List addObject:@"职业:"];
    NSString *modelsBloodtype = [self.dataDic objectForKey:@"economicBloodtype"];
    {
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = modelsBloodtype;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }

    //现居城市
    {
        rect.origin.x = rect.origin.x + 20;
        rect.size.width = rect.size.width - 20;
        [self.section1List addObject:@"所在城市:"];
        NSString *temp = [self.dataDic objectForKey:@"economicAddress"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }
    
    //所属公司
    {
        rect.size.width = rect.size.width + 100;
        [self.section1List addObject:@"所属公司:"];
        NSString *temp = [self.dataDic objectForKey:@"economicSsgs"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }


    //个人风格
  
    self.modelsStyle = [self.dataDic objectForKey:@"economicStyle"];
    {
        static NSString *Cell2 = @"Cell2";
        TextFieldTableViewCell *cell = [[[TextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell2] autorelease];
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textField.layer.cornerRadius=4.0f;
        cell.textField.layer.masksToBounds=YES;
        cell.textField.backgroundColor=[UIColor lightGrayColor];
        cell.textField.layer.borderColor=[[UIColor grayColor]CGColor];
        cell.textField.layer.borderWidth= 1.0f;
        cell.textField.text = self.modelsStyle;
        [self.inputList addObject:cell];
    }
    
    //个人介绍
    self.modelsPersent = [self.dataDic objectForKey:@"economicPersent"];
    {
        static NSString *Cell3 = @"Cell3";
        TextFieldTableViewCell *cell = [[[TextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell3] autorelease];
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textField.layer.cornerRadius=4.0f;
        cell.textField.layer.masksToBounds=YES;
        cell.textField.backgroundColor=[UIColor lightGrayColor];
        cell.textField.layer.borderColor=[[UIColor grayColor]CGColor];
        cell.textField.layer.borderWidth= 1.0f;
        cell.textField.text = self.modelsPersent;
        [self.inputList addObject:cell];
    }
    
    
    self.section4List = [[[NSMutableArray alloc] init] autorelease];
    //工作电话
    {
        rect.size.width = rect.size.width - 50;
        [self.section4List addObject:@"工作电话:"];
        NSString *temp = [self.dataDic objectForKey:@"economicPhone"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }
    
    //工作邮箱
    {
        [self.section4List addObject:@"工作邮箱:"];
        NSString *temp = [self.dataDic objectForKey:@"economicEmail"];
        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
        textField.text = temp;
        textField.delegate = self;
        mTextFieldTagIndex++;
        textField.tag = mTextFieldTagIndex;
        textField.borderStyle=UITextBorderStyleNone;
        textField.layer.cornerRadius=4.0f;
        textField.layer.masksToBounds=YES;
        textField.backgroundColor=[UIColor lightGrayColor];
        textField.layer.borderColor=[[UIColor grayColor]CGColor];
        textField.layer.borderWidth= 1.0f;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        textField.returnKeyType = UIReturnKeyNext;
        [self.textFieldList1 addObject:textField];
    }
    
//    //工作QQ
//    {
//        [self.section4List addObject:@"工作QQ:"];
//        NSString *temp = [self.dataDic objectForKey:@"economicQq"];
//        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
//        textField.text = temp;
//        textField.delegate = self;
//        mTextFieldTagIndex++;
//        textField.tag = mTextFieldTagIndex;
//        textField.borderStyle=UITextBorderStyleNone;
//        textField.layer.cornerRadius=4.0f;
//        textField.layer.masksToBounds=YES;
//        textField.backgroundColor=[UIColor lightGrayColor];
//        textField.layer.borderColor=[[UIColor grayColor]CGColor];
//        textField.layer.borderWidth= 1.0f;
//        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
//        textField.returnKeyType = UIReturnKeyNext;
//        [self.textFieldList1 addObject:textField];
//    }
//    
//    //工作微信
//    {
//        [self.section4List addObject:@"工作微信:"];
//        NSString *temp = [self.dataDic objectForKey:@"economicVx"];
//        UITextField *textField = [[[UITextField alloc] initWithFrame:rect] autorelease];
//        textField.text = temp;
//        textField.delegate = self;
//        mTextFieldTagIndex++;
//        textField.tag = mTextFieldTagIndex;
//        textField.borderStyle=UITextBorderStyleNone;
//        textField.layer.cornerRadius=4.0f;
//        textField.layer.masksToBounds=YES;
//        textField.backgroundColor=[UIColor lightGrayColor];
//        textField.layer.borderColor=[[UIColor grayColor]CGColor];
//        textField.layer.borderWidth= 1.0f;
//        textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
//        textField.returnKeyType = UIReturnKeyNext;
//        [self.textFieldList1 addObject:textField];
//    }

    //演出经历
    self.modelsExperience = [self.dataDic objectForKey:@"economicExperience"];
    {
        static NSString *Cell5 = @"Cell5";
        TextFieldTableViewCell *cell = [[[TextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell5] autorelease];
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textField.layer.cornerRadius=4.0f;
        cell.textField.layer.masksToBounds=YES;
        cell.textField.backgroundColor=[UIColor lightGrayColor];
        cell.textField.layer.borderColor=[[UIColor grayColor]CGColor];
        cell.textField.layer.borderWidth= 1.0f;
        cell.textField.text = self.modelsExperience;
        [self.inputList addObject:cell];
    }
}

//flag:1--头像  2--大图
- (void)updateImage:(UIImage *)editedImage andFlag:(int)flag
{
    self.saveImage = editedImage;
    FMManager *fm = [FMManager sharedInstance];
    NSString *userId =fm.userId;
    NSString *userType = @"3";
    NSString *modelId = @"0";
    //"photoType可以是1:头像 2:大图 图片放到http body里面"
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@?userId=%@&photoType=%d&userType=%@&modelsId=%@",kHttpHost,kUploadModelsAvatarstr,userId,flag,userType,modelId];
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.timeOutSeconds = 180;
    
    NSData *imageData = UIImageJPEGRepresentation(editedImage,1.0);
    [request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"file"];
    [request setRequestMethod:@"POST"];
    request.delegate = self;
    [request startAsynchronous];
    
    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:mHUD];
    mHUD.delegate = self;
    [mHUD show:YES];
}

-(void)updatePhoto:(NSString *)photoName
{
    FMManager *fm = [FMManager sharedInstance];
    NSString *userId = fm.userId;
    NSNumber *num = [NSNumber numberWithInt:[userId intValue]];
    
    NSMutableDictionary *coDic = [[[NSMutableDictionary alloc] init] autorelease];
    [coDic setObject:num forKey:@"userId"];
    [coDic setObject:photoName forKey:@"photoName"];
    
    NSString *postStr = [coDic JSONString];
    DLog(@"postStr = %@",postStr);
    
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",kHttpHost,kUpdatateNomodelHeadImage];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
    request.delegate = self;
    [request startAsynchronous];
}
-(void)deleteImageByid:(NSString *)photoName
{
    FMManager *fm = [FMManager sharedInstance];
    NSString *userId = fm.userId;
    NSNumber *num = [NSNumber numberWithInt:[userId intValue]];
    
    NSMutableDictionary *coDic = [[[NSMutableDictionary alloc] init] autorelease];
    [coDic setObject:num forKey:@"userId"];
    [coDic setObject:photoName forKey:@"photoName"];
    
    NSString *postStr = [coDic JSONString];
    DLog(@"postStr = %@",postStr);
    
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",kHttpHost,kDelNomodelPhoto];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
    request.delegate = self;
    [request startAsynchronous];
}

-(void)toModifeModel
{
    EditViewController *ctrl = [[[EditViewController alloc] init] autorelease];
    ctrl.delegate = self;
    //[ctrl initWithStyle:UITableViewStyleGrouped];
    ctrl.dataDic = [NSMutableDictionary dictionaryWithDictionary:self.curModelDic];
    ctrl.title = @"编辑";
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(void)toAddImage:(int)flag
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.allowsEditing = NO;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentModalViewController:picker animated:YES];
    [picker release];
    //self.window.rootViewController = picker;
    //[picker release];
    
    self.library = [[[ALAssetsLibrary alloc] init] autorelease];
    self.imageEditor = [[[DemoImageEditor alloc] initWithNibName:@"DemoImageEditor" bundle:nil] autorelease];
    
    self.imageEditor.doneCallback = ^(UIImage *editedImage, BOOL canceled){
        if(!canceled)
        {
            [picker dismissModalViewControllerAnimated:YES];
            [self updateImage:editedImage andFlag:flag];
        }
        else
        {
            [picker popToRootViewControllerAnimated:YES];
            [picker setNavigationBarHidden:NO animated:YES];
        }
    };    
}

-(void) reAddModel:(NSString *)modelId
{
    //for test
    self.curModelDic = [[[NSMutableDictionary alloc] init] autorelease];
    [self.curModelDic setObject:modelId forKey:@"modelsId"];
    NSString *userid = [[FMManager sharedInstance] userId];
    [self.curModelDic setObject:userid forKey:@"userId"];
    [self toModifeModel];
}

#pragma mark - Table view data source

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == 2)
    {
        return self.section2headerView;
    }
    else if(section == 3)
    {
         return self.section3headerView;
    }
    else if(section == 4)
    {
        return self.section4headerView;
    }
    else if(section == 5)
    {
        return self.section5headerView;
    }
    
    return nil;
}

-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 2)
    {
        return self.section2headerView.frame.size.height;
    }
    else if(section == 3)
    {
         return self.section3headerView.frame.size.height;
    }
    else if(section == 4)
    {
       return self.section4headerView.frame.size.height;
    }
    else if(section == 5)
    {
        return self.section5headerView.frame.size.height;
    }
    
    return 0.0;
}


-(float) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if( indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            return 110.0;
        }
        else
        {
            float space = (indexPath.row == 1) ? 6.0 : 4.0;
            return ((tableView.frame.size.width - 10) / 4.0 + space);
        }
    }
    else if(indexPath.section == 1 || indexPath.section == 4)
    {
        return 36;
    }
    else if(indexPath.section == 2 || indexPath.section == 3)
    {
        return 88;
    }
    else if(indexPath.section == 5)
    {
        return 140;
    }
    
    return 44;
}

-(float) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    /*
    if(section == 0)
    {
        return self.section0footView.frame.size.height;
    }
     */
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    /*
    FMManager *fm = [FMManager sharedInstance];
    if( [fm.modeIdentify isEqualToString:@"1"] || [fm.modeIdentify isEqualToString:@"2"])
    {
        return 6;
    }
    else
    {
        return 7;
    }
     */
    
    return 6;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if(section == 0)
    {
        NSArray *array = self.section0list;
        int mol = array.count % kImageIntNums;
        int size = ([array count] )/kImageIntNums + 1;
        if(mol == 0)
        {
            size--;
        }
        DLog(@"section0list section = %d,size = %d",size,array.count);
        return (size + 1);
    }
    else if(section == 1)
    {
        return self.section1List.count;
    }
    else if(section == 2 || section == 3 || section == 5)
    {
        return 1;
    }
    else if(section == 4)
    {
        return self.section4List.count;
    }
    
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    /*
    if(section == 0)
    {
        return self.section0footView;
    }
    */
    
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = [indexPath row];
    int section = [indexPath section];
    DLog(@"section = %d",section);
    
    if(section == 0)
    {
        if(row == 0)
        {
            static NSString *personalHeaderCell = @"personalHeaderCell";
            PersonalHeadShowCell *cell = [tableView dequeueReusableCellWithIdentifier:personalHeaderCell];
            if (cell == nil)
            {
                cell = [[[PersonalHeadShowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:personalHeaderCell] autorelease];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.delegate = self;
            cell.headerImageView.tag = kEconomicHeaderImageTag;
            
            FMManager *fm = [FMManager sharedInstance];
            if(!self.economicAvatarstr || self.economicAvatarstr.length == 0)
            {
                [cell.headerImageView setImage:[UIImage imageNamed:@"addphotos.png"]];
            }
            else
            {
                NSString *imageUrl = [NSString stringWithFormat:@"%@/%@/%@/small_%@",kHttpHost,@"photo",fm.userId,self.economicAvatarstr];
                DLog(@"head url : %@",imageUrl);
                [cell.headerImageView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"addphotos.png"]];
            }
            
            return cell;
        }
        else
        {
            static NSString *CellIdentifier0 = @"Cell0";
            ThreeImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
            if(cell == nil)
            {
                cell = [[[ThreeImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0 canCheck:NO] autorelease];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            NSArray *array = self.section0list;
            int index;
            int listSize = [array count] - 1;
            DLog(@"listSize = %d",listSize);
            for( int i = 0; i < kImageIntNums; i++)
            {
                index = (row-1) *kImageIntNums +i;
                TouchImageView *imageView = [cell.imageViews objectAtIndex:i];
                
                if (row > 1)
                {
                    CGRect rect = imageView.frame;
                    rect.origin.y = 0.0;
                    imageView.frame = rect;
                }
                
                //UIView *canCheckView = [imageView viewWithTag:kCanCheckTag];
                if(index <= listSize)
                {
                    KeyValueObject *item = [array objectAtIndex:index];;
                    if([item.key isEqualToString:@"add"])
                    {
                        [imageView setImage:[UIImage imageNamed:item.keyDispName]];
                    }
                    else
                    {
                        [imageView initialize];
                        NSString *imageUrl = [NSString stringWithFormat:@"%@/photo/%@/small_%@",kHttpHost,[[FMManager sharedInstance] userId],item.keyDispName];
                        DLog(@"imageUrl = %@",imageUrl);
                        [imageView setImageWithURL:[NSURL URLWithString:imageUrl]
                                  placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
                        
                    }
                    imageView.delegate = self;
                    imageView.tag = index + kImageViewTagBase;
                    imageView.hidden = NO;
                    
                }
                else
                {
                    imageView.hidden = YES;
                }
                
            }
            return cell;
            
        }
        
    }
    else if( section == 1)
    {
        static NSString *Cell1 = @"Cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if(cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell1] autorelease];
            //cell.textLabel.textColor = [UIColor grayColor];
            cell.textLabel.font =[UIFont systemFontOfSize:15];
            cell.textLabel.textAlignment = UITextAlignmentLeft;
        }
        cell.textLabel.text = [self.section1List objectAtIndex:indexPath.row];
        int row = indexPath.row;
        int tag = kTextFieldTagBase + row;
        UIView *view = [cell viewWithTag:tag];
        if(view)
        {
            [view removeFromSuperview];
        }
        [cell addSubview:[self.textFieldList1 objectAtIndex:row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    else if( section == 2)
    {
        static NSString *Cell2_ = @"Cell2";
        TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell2_];
        if(cell == nil)
        {
            cell = [self.inputList objectAtIndex:0];
            //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell2] autorelease];
        }
        return cell;
        
    }
    else if( section == 3)
    {
        static NSString *Cell3_ = @"Cell3";
        TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell3_];
        if(cell == nil)
        {
            cell = [self.inputList objectAtIndex:1];
        }
        return cell;
    }
    else if( section == 4)
    {
        static NSString *Cell4 = @"Cell4";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell4];
        if(cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Cell4] autorelease];
            //cell.textLabel.textColor = [UIColor grayColor];
            cell.textLabel.font =[UIFont systemFontOfSize:15];
            cell.textLabel.textAlignment = UITextAlignmentLeft;
        }
        cell.textLabel.text = [self.section4List objectAtIndex:indexPath.row];
        int index = 9 -5 + indexPath.row;
        
        int tag = kTextFieldTagBase + index;
        UIView *view = [cell viewWithTag:tag];
        if(view)
        {
            [view removeFromSuperview];
        }
        
        [cell addSubview:[self.textFieldList1 objectAtIndex:index]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if( section == 5)
    {
        static NSString *Cell5_ = @"Cell5";
        TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell5_];
        if(cell == nil)
        {
            cell = [self.inputList objectAtIndex:2];
        }
        return cell;
    }

    
    return nil;

}

#pragma mark - TouchImageViewDelegate

-(void) clickImageView:(TouchImageView *) imageView
{
    if(imageView.tag == kEconomicHeaderImageTag)//经纪人头像
    {
        [self toAddImage:1];
    }
    else
    {
        int i = imageView.tag  - kImageViewTagBase;
        KeyValueObject *item = [self.section0list objectAtIndex:i];
        if ([item.key isEqualToString:@"add"])
        {
            [self toAddImage:2];
        }
        
//        int i = imageView.tag  - kImageViewTagBase;
//        KeyValueObject *item = [self.section0list objectAtIndex:i];
//        if([item.key isEqualToString:@"add"])
//        {
//            NSString *poststr = [[FMManager sharedInstance] userId];
//            poststr = [NSString stringWithFormat:@"{\"userId\":%@}",poststr];
//            NSString *urlstr = [NSString stringWithFormat:@"%@/%@",kHttpHost,kAddModelsInfo];
//            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlstr]];
//            [request appendPostData:[poststr dataUsingEncoding:NSUTF8StringEncoding]];
//            [request setRequestMethod:@"POST"];
//            [request setDelegate:self];
//            [request startAsynchronous];
//        }
//        else
//        {
//            NSArray *modelList = [self.dataDic objectForKey:@"modelsList"];
//            self.curModelDic = [modelList objectAtIndex:i];
//            [self toModifeModel];
//        }
    }
}

-(void) viewListing:(TouchImageView *)imageView
{
    int i = imageView.tag - kImageViewTagBase;
    mSelDeleteImageIndex = i;
    KeyValueObject *item = [self.section0list objectAtIndex:i];
    [self updatePhoto:item.keyDispName];
}

//删除图片
-(void) editListing:(TouchImageView *)imageView
{
    int i = imageView.tag  - kImageViewTagBase;
    mSelDeleteImageIndex = i;
    KeyValueObject *item = [self.section0list objectAtIndex:i];
    [self deleteImageByid:item.keyDispName];
}


#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    int index = textField.tag - kTextFieldTagBase;
    if(index < (self.textFieldList1.count - 1))
    {
        int i = index +1;
        UITextField *tField = [self.textFieldList1 objectAtIndex:i];
        [tField becomeFirstResponder];
    }
    else {
        [textField resignFirstResponder];
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
    mCurTextField  = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self saveCurTextField:textField];
}


- (void)saveCurTextField:(UITextField *)textField
{
    if(textField)
    {
        int index = textField.tag - kTextFieldTagBase;
        DLog(@"%d",index);
        //KeyValueObject *item = [self.textFieldDataList objectAtIndex:index];
        //item.valueDispName = textField.text;
    }
}

#pragma mark - ASIHTTPRequestDelegate
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *urlStr = [NSString stringWithFormat:@"%@",request.url];
    
    NSString *action = @"";
    if (urlStr.length > 0)
    {
        NSArray *array = [urlStr componentsSeparatedByString:@"/"];
        action = [array objectAtIndex:(array.count - 1)];
    }
    DLog(@"%@",action);
    
    if([action isEqualToString:kUpdateNomodelDatas])
    {
        NSData *data = [request responseData];
        NSArray *array = [data objectFromJSONData];
        if(array != nil && array.count > 0)
        {
            NSString *item0 = [array objectAtIndex:0];
            if([item0 isEqualToString:kResponseOk])
            {
                //NSDictionary *datadic = [array objectAtIndex:1];
                
                if(mHUD == nil)
                {
                    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                    [self.navigationController.view addSubview:mHUD];
                    mHUD.delegate = self;
                    [mHUD show:YES];
                }
                mHUD.mode =MBProgressHUDModeText;
                mHUD.labelText = @"保存成功";
                [mHUD hide:YES afterDelay:1.0];
                
                if(delegate)
                {
                    [delegate refeashData2];
                    /*
                    [self.saveDataDic setObject:[self.dataDic objectForKey:@"modelsAvatarstr"] forKey:@"modelsAvatarstr"];
                    [self.saveDataDic setObject:self.modelsPhotostr forKey:@"modelsPhotostr"];
                    [delegate saveData:self.saveDataDic];
                     */
                }
            }
        }
        else
        {
            if(mHUD == nil)
            {
                mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                [self.navigationController.view addSubview:mHUD];
                mHUD.delegate = self;
                [mHUD show:YES];
            }
            mHUD.mode =MBProgressHUDModeText;
            mHUD.labelText = @"保存失败";
            [mHUD hide:YES afterDelay:1.5];
        }
    }
    else if( [action isEqualToString:kDelNomodelPhoto])
    {
        NSData *data = [request responseData];
        NSArray *array = [data objectFromJSONData];
        if(array != nil && array.count > 0)
        {
            NSString *item0 = [array objectAtIndex:0];
            if([item0 isEqualToString:kResponseOk])
            {
                [self.section0list removeObjectAtIndex:mSelDeleteImageIndex];
                
                if(mHUD == nil)
                {
                    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                    [self.navigationController.view addSubview:mHUD];
                    mHUD.delegate = self;
                    [mHUD show:YES];
                }
                mHUD.mode =MBProgressHUDModeText;
                mHUD.labelText = @"删除成功";
                [mHUD hide:YES afterDelay:1.0];
                
                KeyValueObject *temp = [self.section0list objectAtIndex:(self.section0list.count - 1)];
                if(![temp.key isEqualToString:@"add"])
                {
                    KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
                    item.key = @"add";
                    item.keyDispName = @"addphotos.png";
                    [self.section0list addObject:item];
                }
                
                if(delegate)
                {
                    [delegate refeashData2];
                }
                [self.tableView reloadData];
            }
            else if([item0 isEqualToString:kResponseOk3])
            {
                if(array.count > 1)
                {
                    NSString *item1 = [array objectAtIndex:1];
                    if(mHUD == nil)
                    {
                        mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                        [self.navigationController.view addSubview:mHUD];
                        mHUD.delegate = self;
                        [mHUD show:YES];
                    }
                    mHUD.mode =MBProgressHUDModeText;
                    mHUD.labelText = item1;
                    [mHUD hide:YES afterDelay:1.0];
                }
                
            }
        }
        else
        {
            if(mHUD == nil)
            {
                mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                [self.navigationController.view addSubview:mHUD];
                mHUD.delegate = self;
                [mHUD show:YES];
            }
            mHUD.mode =MBProgressHUDModeText;
            mHUD.labelText = @"删除成功,返回结果失败";
            [mHUD hide:YES afterDelay:1.5];
        }
    }
    else if([action isEqualToString:kUpdatateNomodelHeadImage])
    {
        NSData *data = [request responseData];
        NSArray *array = [data objectFromJSONData];
        if(array != nil && array.count > 0)
        {
            NSString *item0 = [array objectAtIndex:0];
            if([item0 isEqualToString:kResponseOk])
            {
                if(mHUD == nil)
                {
                    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                    [self.navigationController.view addSubview:mHUD];
                    mHUD.delegate = self;
                    [mHUD show:YES];
                }
                mHUD.mode =MBProgressHUDModeText;
                mHUD.labelText = @"成功设置头像";
                [mHUD hide:YES afterDelay:1.0];
                
                if(self.section0list.count == 2)
                {
                    
                }
                else
                {
                    KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
                    KeyValueObject *temp = [self.section0list objectAtIndex:mSelDeleteImageIndex];
                    item.key = temp.key;
                    item.keyDispName = temp.keyDispName;
                    [self.section0list removeObjectAtIndex:mSelDeleteImageIndex];
                    [self.section0list insertObject:item atIndex:0];
                    
                    self.economicAvatarstr = item.keyDispName;
                    
                    [self.tableView reloadData];
                    
                    if(delegate)
                    {
                        [delegate refeashData2];
                    }
                }
            }
        }
        else
        {
            if(mHUD == nil)
            {
                mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                [self.navigationController.view addSubview:mHUD];
                mHUD.delegate = self;
                [mHUD show:YES];
            }
            mHUD.mode =MBProgressHUDModeText;
            mHUD.labelText = @"设置头像 失败";
            [mHUD hide:YES afterDelay:1.5];
        }
    }
    else if ([action isEqualToString:kAddModelsInfo])
    {
        NSData *data = [request responseData];
        NSArray *result = [data objectFromJSONData];
        if (result.count > 0)
        {
            NSString *array0 = [result objectAtIndex:0];
            if ([array0 isEqualToString:kResponseOk])
            {
                if (result.count > 1)
                {
                    NSDictionary *dic = [result objectAtIndex:1];
                    NSString *modelsId = [dic objectForKey:@"modelsId"];
                    
                    //调用添加模特的方法
                    [self reAddModel:modelsId];
                }
            }
        }
    }
    else    //上传图片
    {
        NSData *data = [request responseData];
        NSArray *array = [data objectFromJSONData];
        if(array != nil && array.count > 0)
        {
            NSString *item0 = [array objectAtIndex:0];
            if([item0 isEqualToString:kResponseOk])
            {
                if (action.length > 0)
                {
                    NSArray *photoTypeArray = [action componentsSeparatedByString:@"&"];
                    if (photoTypeArray.count > 1)
                    {
                        NSString *photoRequestType = [photoTypeArray objectAtIndex:1];
                        if (photoRequestType && photoRequestType.length > 0)
                        {
                            photoTypeArray = [photoRequestType componentsSeparatedByString:@"="];
                            if (photoTypeArray.count > 1)
                            {
                                photoRequestType = [photoTypeArray objectAtIndex:1];
                                
                                if (photoRequestType && photoRequestType.length > 0)
                                {
                                    if ([photoRequestType isEqualToString:@"1"])
                                    {
                                        NSString *photoId = [array objectAtIndex:1];
                                        self.economicAvatarstr = photoId;
                                        
                                        DLog("photoId = %@",photoId);
                                        [self.saveDataDic setObject:self.economicAvatarstr forKey:@"economicAvatarstr"];
                                    }
                                    else
                                    {
                                        NSString *photoId = [array objectAtIndex:1];
                                        FMManager *fm = [FMManager sharedInstance];
                                        
                                        NSString *imageUrl = [NSString stringWithFormat:@"%@/photo/%@/%@",kHttpHost,fm.userId,photoId];
                                        [[SDImageCache sharedImageCache] storeImage:self.saveImage forKey:imageUrl];
                                        
                                        KeyValueObject *obj = [[[KeyValueObject alloc] init] autorelease];
                                        obj.canClick = YES;
                                        obj.isCheck = NO;
                                        obj.key = photoId;
                                        obj.keyDispName = photoId;
                                        if(self.section0list && (self.section0list.count != 0))
                                        {
                                            [self.section0list insertObject:obj atIndex:(self.section0list.count - 1)];
                                        }
                                        else
                                        {
                                            self.section0list = [[[NSMutableArray alloc] init] autorelease];
                                            [self.section0list addObject:obj];
                                        }
                                        
                                        if(self.section0list.count <= 8)
                                        {
                                            KeyValueObject *temp = [self.section0list objectAtIndex:(self.section0list.count - 1)];
                                            if(![temp.key isEqualToString:@"add"])
                                            {
                                                KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
                                                item.key = @"add";
                                                item.keyDispName = @"addphotos.png";
                                                [self.section0list addObject:item];
                                            }
                                        }
                                        else
                                        {
                                            [self.section0list removeObjectAtIndex:(self.section0list.count - 1)];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                
                [self.tableView reloadData];
                
                
                if(mHUD == nil)
                {
                    mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                    [self.navigationController.view addSubview:mHUD];
                    mHUD.delegate = self;
                    [mHUD show:YES];
                }
                mHUD.mode =MBProgressHUDModeText;
                mHUD.labelText = @"保存图片成功";
                [mHUD hide:YES afterDelay:1.0];
                
                if(delegate)
                {
                    [self.delegate refeashData2];
                }
            }
        }
        else
        {
            if(mHUD == nil)
            {
                mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
                [self.navigationController.view addSubview:mHUD];
                mHUD.delegate = self;
                [mHUD show:YES];
            }
            mHUD.mode =MBProgressHUDModeText;
            mHUD.labelText = @"保存图片失败";
            [mHUD hide:YES afterDelay:1.5];
        }
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    if (error)
    {
        if(mHUD == nil)
        {
            mHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
            [self.navigationController.view addSubview:mHUD];
            mHUD.delegate = self;
            [mHUD show:YES];
        }
        mHUD.mode =MBProgressHUDModeText;
        mHUD.labelText = @"网络链接失败";
        [mHUD hide:YES afterDelay:1.5];
    }
}

#pragma mark -
#pragma mark MBProgressHUDDelegate methods

- (void)hudWasHidden:(MBProgressHUD *)hud {
	// Remove HUD from screen when the HUD was hidded
	[mHUD removeFromSuperview];
	[mHUD release];
	mHUD = nil;
}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate methods
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image =  [info objectForKey:UIImagePickerControllerOriginalImage];
    NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    
    [self.library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
        UIImage *preview = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
        
        self.imageEditor.sourceImage = image;
        self.imageEditor.previewImage = preview;
        [self.imageEditor reset:NO];
        
        [picker pushViewController:self.imageEditor animated:YES];
        [picker setNavigationBarHidden:YES animated:NO];
        
    } failureBlock:^(NSError *error) {
        //NSLog(@"Failed to get asset from library");
    }];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark EditViewControllerDelegate

-(void)updateEdit2ModelDatas:(NSMutableDictionary *)datas
{
    NSArray *modelLists = [self.dataDic objectForKey:@"modelsList"];
    NSMutableArray *modelMutLists = [NSMutableArray arrayWithArray:modelLists];
    
    if (self.dataDic)
    {
        if (modelMutLists && modelMutLists.count > 0)
        {
            int flag = -1;
            NSString *newModelId = [NSString stringWithFormat:@"%@",[datas objectForKey:@"modelsId"]];
            for (int i=0; i<modelMutLists.count; i++)
            {
                NSDictionary *dic = [modelMutLists objectAtIndex:i];
                NSString *oldModelId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"modelsId"]];
                if ([oldModelId isEqualToString:newModelId])
                {
                    [modelMutLists insertObject:datas atIndex:i];
                    [modelMutLists removeObjectAtIndex:(i+1)];
                    flag = i;
                    break;
                }
            }
            
            if (flag == -1)
            {
                [modelMutLists addObject:datas];
            }
        }
        else
        {
            [modelMutLists addObject:datas];
        }
        
        [self.dataDic setValue:modelMutLists forKey:@"modelsList"];
        NSString *writeString = [self.dataDic JSONString];
        [FMManager savePersonalMessageToLocal:writeString];
    }
    
    self.section0list = [[[NSMutableArray alloc] init] autorelease];
    
    //section 0 图片
    if(modelMutLists && modelMutLists.count > 0)
    {
        for( int i=0; i<modelMutLists.count; i++)
        {
            KeyValueObject *item = [[KeyValueObject alloc] init];
            item.canClick = YES;
            item.isCheck = NO;
            item.key = [NSString stringWithFormat:@"%d",i];
            
            NSDictionary *dicdata = [modelMutLists objectAtIndex:i];
            NSString *imageUrl = [NSString stringWithFormat:@"%@/%@/%@/small_%@",kHttpHost,@"photo",self.edit2UserId,[dicdata objectForKey:@"modelsAvatarstr"]];
            item.keyDispName = imageUrl;
            DLog(@"imageurl = %@",imageUrl);
            [self.section0list addObject:item];
            [item release];
        }
    }
    
    if(self.section0list.count < kMaxPhotoNumber)
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"add";
        item.keyDispName = @"addphotos2.png";
        [self.section0list addObject:item];
    }
    
    [self.tableView reloadData];
}

-(void)refeashData
{
    
}

#pragma mark -
#pragma mark PersonalHeadShowCellDelegate
//点击头像
- (void)clickedHeaderImage
{
    [self toAddImage:1];
}

@end